<?php
/**
 * $Id: by_socket.php 11 2010-11-24 22:10:53Z LEON $
 * $Author: LEON $
 * $LastChangedDate: 2010-11-24 22:10:53 +0000 (Wed, 24 Nov 2010) $
 * $Revision: 11 $
 */
namespace cmFly\utils\cli_singleton;
class by_socket {
    protected $sock;
    protected $port;
    public function __construct($port) {
	$this->set_port($port);
	$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    }
    protected function set_port($port) {
	$this->port=$port;
    }
    public function lock() {
	return socket_bind($this->sock, 'localhost', $this->port);
    }
    public function unlock() {
	socket_close($this->sock);
	$this->sock=NULL;
    }
    public function __destruct() {
	if($this->sock) {
	    $this->unlock();
	}
    }
}